<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动开始时间" prop="statdate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.statdate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择活动开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="活动结束时间" prop="enddate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.enddate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择活动结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="活动状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择活动状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vote:vote:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['vote:vote:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vote:vote:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['vote:vote:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="voteList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动id" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" >
        <template slot-scope="scope">
          <router-link :to="'/vote/voteItem/' + scope.row.token" class="link-type">
            <span>{{ scope.row.title }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="picurl">
        <template slot-scope="scope">
          <el-image :src="scope.row.picurl" alt=""></el-image>
        </template>
      </el-table-column>
      <el-table-column label="活动开始时间" align="center" prop="statdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.statdate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动结束时间" align="center" prop="enddate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enddate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="限制投票数" align="center" prop="allLimitNum" />
      <el-table-column label="限制每天投票数" align="center" prop="dayLimitNum" />
      <el-table-column label="限制选项每天投票数" align="center" prop="selecterLimitNum" />
      <el-table-column label="活动状态" align="center" prop="status" :formatter="statusFormat" />
     <el-table-column label="是否需要关注公众号" align="center" prop="subscribe" :formatter="subscribeFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vote:vote:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vote:vote:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改投票活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="活动token" prop="title">
          <el-input v-model="form.token" placeholder="请输入16位token，不填则自动生成" />
        </el-form-item>
        <el-form-item label="关键词" prop="keyword">
          <el-input v-model="form.keyword" placeholder="请输入关键词" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input v-model="form.type" placeholder="请输入类型" />
        </el-form-item>
        <el-form-item label="图片">
          <imageUpload v-model="form.picurl"/>
        </el-form-item>
        <el-form-item label="是否显示图片">
          <el-radio-group v-model="form.showpic">
            <el-radio
              v-for="dict in showpicOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="活动简介">
          <editor v-model="form.info" :min-height="192"/>
        </el-form-item>
        <el-form-item label="奖项设置">
          <editor v-model="form.prizeInfo" :min-height="192"/>
        </el-form-item>
        <el-form-item label="投票规则">
          <editor v-model="form.voteRoles" :min-height="192"/>
        </el-form-item>
        <el-form-item label="活动开始时间" prop="statdate">
          <el-date-picker clearable size="small"
                          v-model="form.statdate"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择活动开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="enddate">
          <el-date-picker clearable size="small"
                          v-model="form.enddate"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择活动结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="限制投票数" prop="allLimitNum">
          <el-input v-model="form.allLimitNum" placeholder="请输入整数" />
          <el-row>请输入限制每个用户每个活动总的投票数，超过不让投票,0表示不限制,默认为0,请输入整数</el-row>
        </el-form-item>
        <el-form-item label="限制每天投票数" prop="dayLimitNum">
          <el-input v-model="form.dayLimitNum" placeholder="请输入整数" />
          <el-row>限制每个用户每个活动每天的投票数，超过不让投票,0表示不限制,默认为0,请输入整数</el-row>
        </el-form-item>
        <el-form-item label="限制选项每天投票数" prop="selecterLimitNum">
          <el-input v-model="form.selecterLimitNum" placeholder="请输入整数" />
          <el-row>限制每个选项每天获得的票数,0表示不限制,1表示每个用户每天只能给该选项投1票,默认为0,请输入整数</el-row>
        </el-form-item>
        <el-form-item label="是否需要关注公众号">
          <el-radio-group v-model="form.subscribe">
            <el-radio
              v-for="dict in showpicOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="活动状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import { listVote, getVote, delVote, addVote, updateVote } from "@/api/vote/vote";
    import ImageUpload from '@/components/ImageUpload';
    import Editor from '@/components/Editor';

    export default {
        name: "Vote",
        components: {
            ImageUpload,
            Editor
        },
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 投票活动表格数据
                voteList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 是否显示图片字典,是否需要关注公众号字典
                showpicOptions: [],
                // 活动状态字典
                statusOptions: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    title: null,
                    type: null,
                    picurl: null,
                    info: null,
                    statdate: null,
                    enddate: null,
                    status: null,
                },
                // 表单参数
                form: {
                    allLimitNum:0,
                    dayLimitNum:0,
                    selecterLimitNum:0
                },
                // 表单校验
                rules: {
                    title: [
                        { required: true, message: "标题不能为空", trigger: "blur" }
                    ],
                    statdate: [
                        { required: true, message: "活动开始时间不能为空", trigger: "blur" }
                    ],
                    enddate: [
                        { required: true, message: "活动结束时间不能为空", trigger: "blur" }
                    ],
                    dayLimitNum: [
                        { required: true, message: "限制每个用户每个活动每天的投票数，超过不让投票,0表示不限制,默认为0,请输入整数不能为空", trigger: "blur" }
                    ],
                    selecterLimitNum: [
                        { required: true, message: "限制每个选项每天获得的票数,0表示不限制,1表示每个用户每天只能给该选项投1票,默认为0,请输入整数不能为空", trigger: "blur" }
                    ],
                    status: [
                        { required: true, message: "活动状态不能为空", trigger: "blur" }
                    ],
                }
            };
        },
        created() {
            this.getList();
            this.getDicts("sys_yes_no").then(response => {
                this.showpicOptions = response.data;
            });
            this.getDicts("sys_normal_disable").then(response => {
                this.statusOptions = response.data;
            });
        },
        methods: {
            /** 查询投票活动列表 */
            getList() {
                this.loading = true;
                listVote(this.queryParams).then(response => {
                    this.voteList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 是否显示图片字典翻译
            showpicFormat(row, column) {
                return this.selectDictLabel(this.showpicOptions, row.showpic);
            },
            //是否需要关注公众号
            subscribeFormat(row, column){
                return this.selectDictLabel(this.showpicOptions, row.subscribe);
            },
            // 活动状态字典翻译
            statusFormat(row, column) {
                return this.selectDictLabel(this.statusOptions, row.status);
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    title: null,
                    keyword: null,
                    token: null,
                    type: null,
                    picurl: null,
                    showpic: 0,
                    info: null,
                    prizeInfo: null,
                    voteRoles: null,
                    statdate: null,
                    enddate: null,
                    allLimitNum: 0,
                    dayLimitNum: 0,
                    selecterLimitNum: 0,
                    status: "0",
                    createTime: null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length!==1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加投票活动";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids
                getVote(id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改投票活动";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    console.log(this.form)
                    if (valid) {
                        if (this.form.id != null) {
                            updateVote(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addVote(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$confirm('是否确认删除投票活动编号为"' + ids + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function() {
                    return delVote(ids);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
            },
            /** 导出按钮操作 */
            handleExport() {
                this.download('vote/vote/export', {
                    ...this.queryParams
                }, `vote_vote.xlsx`)
            }
        }
    };
</script>
